<script setup lang="ts">
import { EillnessTime } from '@/enums/index'
import { ref } from 'vue'
import type { Tillness } from '@/types/consult'
import type { UploaderAfterRead, UploaderFileListItem } from 'vant/lib/uploader/types'
import { uploadPost } from '@/api/consult'
import type { Interceptor } from 'vant/lib/utils'
import { useConsultStore } from '@/stores/index'
import { useRoute, useRouter } from 'vue-router'
import { computed } from 'vue'
import { onMounted } from 'vue'
import { showConfirmDialog } from 'vant'
import { timeOptions, FlagOptions } from '@/constants'
const consultStore = useConsultStore()
const route = useRoute()
const router = useRouter()
// const timeOptions = [
//   {
//     label: '一周内',
//     value: EillnessTime.Week
//   },
//   {
//     label: '一月内',
//     value: EillnessTime.Month
//   },
//   {
//     label: '半年内',
//     value: EillnessTime.HalfYear
//   },
//   {
//     label: '半年以上',
//     value: EillnessTime.More
//   }
// ]
// const FlagOptions = [
//   {
//     label: '就诊过',
//     value: 1
//   },
//   {
//     label: '未就诊过',
//     value: 0
//   }
// ]
const formData = ref<Tillness>({
  illnessDesc: '',
  illnessTime: EillnessTime.Week,
  consultFlag: 1,
  pictures: []
})
// 上传图片
const fileList = ref<UploaderFileListItem[]>([])
const afterRead: UploaderAfterRead = async res => {
  // 排除多张
  //   Array.isArray(res)
  // if (Object.prototype.toString.call(res) === '[object Array]') {
  //   return
  // }
  // 排除了数组
  if (Array.isArray(res)) {
    return
  }
  // 排除无该属性的情况
  if (!res.file) {
    return
  }
  res.status = 'uploading'
  res.message = '上传中....'
  try {
    const uploadRes = await uploadPost(res.file)
    formData.value.pictures.push(uploadRes.data)
    res.status = 'done'
    res.message = ''
    console.log(uploadRes)
  } catch {
    res.status = 'failed'
    res.message = '上传失败'
  }
}
// 上传图片的删除
const beforeDelete: Interceptor = (res, res2) => {
  formData.value.pictures.splice(res2.index, 1)
  return true
}
// 是否禁用下一步
const agree = computed(() => {
  return !!formData.value.illnessDesc
})

const next = () => {
  consultStore.setConsultOrder({
    type: +route.query.type! as 1 | 2 | 3,
    illnessType: +route.query.illnessType! as 0 | 1,
    depId: route.query.depId as string,
    ...formData.value
  })
  router.push('/user/patient?isChange=1')
}

// 打开页面时如果已填写过相应信息，要弹出确认框是否恢复填写的内容
onMounted(async () => {
  if (!consultStore.consultOrder) {
    return
  }
  await showConfirmDialog({
    title: '温馨提示',
    message: '是否恢复之前填写的内容?',
    closeOnPopstate: false
  })
  // 数据恢复
  const { illnessDesc, illnessTime, consultFlag, pictures } = consultStore.consultOrder
  formData.value = { illnessDesc, illnessTime, consultFlag, pictures } as Tillness
  fileList.value = consultStore.consultOrder.pictures!
})
</script>

<template>
  <div class="consult-illness-page">
    <cp-nav-bar title="图文问诊" />
    <!-- 医生提示 -->
    <div class="illness-tip van-hairline--bottom">
      <img class="img" src="@/assets/avatar-doctor.svg" />
      <div class="info">
        <p class="tit">在线医生</p>
        <p class="tip">请描述你的疾病或症状、是否用药、就诊经历，需要我听过什么样的帮助</p>
        <p class="safe"><cp-icon name="consult-safe" /><span>内容仅医生可见</span></p>
      </div>
    </div>
    <!-- 表单 -->
    <div class="illness-form">
      {{ formData }}
      <van-field
        type="textarea"
        rows="3"
        v-model.trim="formData.illnessDesc"
        placeholder="请详细描述您的病情，病情描述不能为空"
      ></van-field>
      <div class="item">
        <p>本次患病多久了？</p>
        <CpRadioBtn :options="timeOptions" v-model="formData.illnessTime"></CpRadioBtn>
        <!-- <cp-radio-btn :options=""  v-model="" /> -->
      </div>
      <div class="item">
        <p>此次病情是否去医院就诊过？</p>
        <CpRadioBtn :options="FlagOptions" v-model="formData.consultFlag"></CpRadioBtn>
        <!-- <cp-radio-btn :options="" v-model="" /> -->
      </div>
      <div class="illness-img">
        <van-uploader
          v-model="fileList"
          upload-icon="photo-o"
          upload-text="上传图片"
          max-count="9"
          :max-size="5 * 1024 * 1024"
          :after-read="afterRead"
          :before-delete="beforeDelete"
        ></van-uploader>
        <p class="tip">上传内容仅医生可见,最多9张图,最大5MB</p>
      </div>
      <!-- 下一步按钮 -->
      <van-button
        :class="{ disabled: !agree }"
        :disabled="!agree"
        @click="next"
        type="primary"
        block
        round
        >下一步</van-button
      >
    </div>
  </div>
</template>

<style lang="scss" scoped>
.consult-illness-page {
  padding-top: 46px;
  .illness-tip {
    display: flex;
    padding: 15px;
    .img {
      width: 52px;
      height: 52px;
      border-radius: 4px;
      overflow: hidden;
      margin-top: 10px;
    }
    .info {
      flex: 1;
      padding-left: 12px;
      .tit {
        font-size: 16px;
        margin-bottom: 5px;
      }
      .tip {
        padding: 12px;
        background: var(--cp-bg);
        color: var(--cp-text3);
        font-size: 13px;
        margin-bottom: 10px;
      }
      .safe {
        font-size: 10px;
        color: var(--cp-text3);
        display: flex;
        align-items: center;
        .cp-icon {
          font-size: 12px;
          margin-right: 2px;
        }
      }
    }
  }
}
.illness-form {
  padding: 15px;
  .van-field {
    padding: 0;
    &::after {
      border-bottom: none;
    }
  }
  .item {
    > p {
      color: var(--cp-text3);
      padding: 15px 0;
    }
  }
}
.illness-img {
  padding-top: 16px;
  margin-bottom: 40px;
  display: flex;
  align-items: center;
  .tip {
    font-size: 12px;
    color: var(--cp-tip);
  }
  ::v-deep() {
    .van-uploader {
      &__preview {
        &-delete {
          left: -6px;
          top: -6px;
          border-radius: 50%;
          background-color: var(--cp-primary);
          width: 20px;
          height: 20px;
          &-icon {
            transform: scale(0.9) translate(-22%, 22%);
          }
        }
        &-image {
          border-radius: 8px;
          overflow: hidden;
        }
      }
      &__upload {
        border-radius: 8px;
      }
      &__upload-icon {
        color: var(--cp-text3);
      }
    }
  }
}
.van-button {
  font-size: 16px;
  margin-bottom: 30px;
  &.disabled {
    opacity: 1;
    background: #fafafa;
    color: #d9dbde;
    border: #fafafa;
  }
}
</style>
